#include "stdafx.h"

extern WSASetLastError_proxy_t pTrueWSASetLastError;

int bind_proxy (
    __in SOCKET           s,
    __in struct sockaddr* addr,
    __in int              addrlen
    )
{
    try
    {
    //SEND
        Header header(GetCurrentThreadId(), "bind");
        bind_in_inout bindIN(s, addr, addrlen);
        
        std::vector<char> out_buffer;
        Serialize(header, bindIN, out_buffer);

        SendToClient (out_buffer);
        
    //RECEIVE()
        bind_out_inout bindOUT;

        std::vector<char> in_buffer;
        
        RecvFromClient (in_buffer, header);
        Deserialize deserialize(in_buffer);
        deserialize(bindOUT);

        if(bindOUT.wsa_last_error_ != 0)
        {
            pTrueWSASetLastError(bindOUT.wsa_last_error_);
            return SOCKET_ERROR;
        }
        return 0;
    }
    catch (std::exception& ex)
    {
        LOG(plog::error) << "bind: " << ex.what();
    }
    pTrueWSASetLastError (WSAEWOULDBLOCK);
    return SOCKET_ERROR;
}
